package com.ruoyi.peach.controller;

import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.peach.domain.Conversations;
import com.ruoyi.peach.service.IConversationsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 会话Controller
 * 
 * @author ruoyi
 * @date 2025-04-24
 */
@RestController
@RequestMapping("/peach/conversations")
public class ConversationsController extends BaseController
{
    @Autowired
    private IConversationsService conversationsService;

    /**
     * 查询会话列表
     */
    @PreAuthorize("@ss.hasPermi('peach:conversations:list')")
    @GetMapping("/list")
    public TableDataInfo list(Conversations conversations)
    {
        System.out.println(conversations.getParams());
        startPage();
        List<Conversations> list = conversationsService.selectConversationsList(conversations);
        return getDataTable(list);
    }
    /**
     * 查询会话列表
     */
    @PreAuthorize("@ss.hasPermi('peach:conversations:list')")
    @GetMapping("/list/app")
    public R listApp(Conversations conversations)
    {

        List<Conversations> list = conversationsService.selectConversationsList(conversations);
        return R.ok(list);
    }

    /**
     * 导出会话列表
     */
    @PreAuthorize("@ss.hasPermi('peach:conversations:export')")
    @Log(title = "会话", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Conversations conversations)
    {
        List<Conversations> list = conversationsService.selectConversationsList(conversations);
        ExcelUtil<Conversations> util = new ExcelUtil<Conversations>(Conversations.class);
        util.exportExcel(response, list, "会话数据");
    }

    /**
     * 获取会话详细信息
     */
    @PreAuthorize("@ss.hasPermi('peach:conversations:query')")
    @GetMapping(value = "/{conversationId}")
    public AjaxResult getInfo(@PathVariable("conversationId") Long conversationId)
    {
        return success(conversationsService.selectConversationsByConversationId(conversationId));
    }

    /**
     * 新增会话
     */
    @PreAuthorize("@ss.hasPermi('peach:conversations:add')")
    @Log(title = "会话", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody Conversations conversations)
    {
       try{
           return R.ok(conversationsService.insertConversations(conversations));
       }catch (Exception e){
           return R.fail("服务器错误");
       }
    }

    /**
     * 修改会话
     */
    @PreAuthorize("@ss.hasPermi('peach:conversations:edit')")
    @Log(title = "会话", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Conversations conversations)
    {
        return toAjax(conversationsService.updateConversations(conversations));
    }

    /**
     * 删除会话
     */
    @PreAuthorize("@ss.hasPermi('peach:conversations:remove')")
    @Log(title = "会话", businessType = BusinessType.DELETE)
	@DeleteMapping("/{conversationIds}")
    public AjaxResult remove(@PathVariable Long[] conversationIds)
    {
        return toAjax(conversationsService.deleteConversationsByConversationIds(conversationIds));
    }
}
